package com.buddy.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;

/**
 * 查看某个文件在HDFS集群的位置
 */
public class FileLoc {

    public static void main(String[] args) throws IOException {
        Configuration configuration = new Configuration();
        FileSystem hdfs = FileSystem.get(configuration);
        Path path = new Path(args[0]);
        FileStatus fileStatus = hdfs.getFileStatus(path);
        BlockLocation[] blockLocations = hdfs.getFileBlockLocations(fileStatus,0,fileStatus.getLen());
        int blockLen = blockLocations.length;
        for (int i = 0;i<blockLen;i++){
            String[] hosts = blockLocations[i].getHosts();
            System.out.println("block "+i+" location:"+hosts[i]);
        }
    }
}
